package com.spyz.P2_combine_file_inputformat_wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * KEYIN，map阶段输入的key：LongWritable(偏移量)
 * VALUEIN, map阶段输入的value：Text(文本内容)
 * KEYOUT, map阶段输出的key：Text(单词)
 * VALUEOUT, map阶段输出的value：IntWritable(1)
 */
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    // 减少重复创建的对象，优化性能
    private Text outK = new Text();
    private IntWritable outV = new IntWritable(1);
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
        // 1、获取一行
        String line = value.toString();

        // 2、对行进行切割
        String[] words = line.split(" ");

        // 3、循环写出
        for (String word : words) {
            outK.set(word);

            context.write(outK,outV);
        }
    }
}
